﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;

namespace UniMana.Win.Training
{
    public partial class SubFrmChooseExcelSheet : DevExpress.XtraEditors.XtraForm
    {
        private string m_strText;
        private DataTable dtDb;
        private bool m_bImport = false;
        private bool m_bOk = false;

        public int m_iSubjectClassKey = 0;
        public int m_iPosition;
        public List<int> m_lScoreType;
        public DataTable m_dtInfo;
        public Guid m_gPersonelKey;

        private bool m_bLockRow = false;
        //private int m_iRowLock = 0;

        public int GetExcelColumns(int _iDBColumns)
        {
            return (int)this.grvDatabaseColumns.GetRowCellValue(_iDBColumns, gclExcel);
        }
        public int GetDBColumns(int _iExcelColumns)
        {
            for (int i = 0; i < dtDb.Rows.Count; i++)
            {
                if (dtDb.Rows[i]["ID"].ToString() == _iExcelColumns.ToString())
                {
                    return i;
                }
            }
            return -1;
        }
        public string LookupEditText
        {
            get {return m_strText;}
        }
        public bool Import
        {
            get { return m_bImport; }
        }
        public bool Ok
        {
            get { return m_bOk; }
        }
        public SubFrmChooseExcelSheet()
        {
            InitializeComponent();
        }
        public void SetDataSource(DataTable dt)
        {
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                if (dt.Columns[i].ColumnName.ToUpper() != "TABLE_NAME")
                {
                    dt.Columns.RemoveAt(i);
                    i--;
                }
            }
            dt.Columns[0].Caption = "Tên bảng";
            this.lueExcelSheet.Properties.DataSource = dt;
            //this.lueExcelSheet.Properties.Columns[0].Caption = "Tên bảng";
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            this.m_strText = this.lueExcelSheet.Text;
            this.m_bOk = true;
            this.Close();
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public void SetDatabaseColumns(DataTable dt)
        {
            DataTable dtColumns = new DataTable();
            dtColumns.Columns.Add("Name");
            dtColumns.Columns.Add("ID",Type.GetType("System.Int16"));
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                dtColumns.Rows.Add(new object[] { dt.Columns[i].ColumnName, -1 });
            }
            this.gctDatabaseColumns.DataSource = dtColumns;
            this.dtDb = dtColumns;
        }
        public void SetRluColumns(DataTable dt)
        {
            DataTable dtColumns = new DataTable();
            dtColumns.Columns.Add("ID", Type.GetType("System.Int16"));
            dtColumns.Columns.Add("Name");
            dtColumns.Rows.Add(new object[] { -1, "Không có" });
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                if (dt.Rows.Count > 1)
                {
                    dtColumns.Rows.Add(new object[] { i, dt.Rows[0][i] });
                }
            }
            this.rluExcel.DataSource = dtColumns;
        }
        private void Nhập_Click(object sender, EventArgs e)
        {
            this.m_bImport = true;
            this.Close();
        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public void SetChooseSheet()
        {
            groupControl2.Visible = true;
            groupControl1.Visible = false;
            panelControl1.Visible = false;
        }
        public void SetChooseData()
        {
            groupControl2.Visible = false;
            groupControl1.Visible = true;
            panelControl1.Visible = true;
        }
        private void grvDatabaseColumns_ShowingEditor(object sender, CancelEventArgs e)
        {
            //if (grvDatabaseColumns.FocusedRowHandle < 6)
            //{
            //    e.Cancel = true;
            //    return;
            //}
            //if (grvDatabaseColumns.FocusedRowHandle % 3 != 0)
            //{
            //    e.Cancel = true;
            //    //return;
            //}
            ////phan quyen tai khoan nhap diem
            //#region [old]
            ////DateTime _dteTimeNow = UniMana.Lib.Sys.SysDateTime.Now;
            ////for (int i = 0; i < m_lScoreType.Count; i++)
            ////{
            ////    UniMana.Lib.Hoa.Training_Score_TaskInputInfo infoTask = new Lib.Hoa.Training_Score_TaskInputInfo(
            ////                                                m_gPersonelKey, m_iSubjectClassKey, _dteTimeNow);
            ////    if (infoTask.Properties.Personnel_PersonnelKey == m_gPersonelKey)
            ////    {
            ////        int k = m_dtInfo.Columns.Count + i * 3;
            ////        if (grvDatabaseColumns.FocusedRowHandle == k)
            ////        {
            ////            Training_SubjectClass_ScoreTypeInfo info = new Training_SubjectClass_ScoreTypeInfo(
            ////                                                        m_iSubjectClassKey, m_lScoreType[i]);
            ////            if (info.Properties.Lock != true)
            ////            {
            ////                e.Cancel = false;
            ////                break;
            ////            }
            ////            else
            ////            {
            ////                e.Cancel = true;
            ////                break;
            ////            }
            ////        }
            ////        else
            ////        {
            ////            e.Cancel = true;
            ////        }
            ////    }
            ////}
            //#endregion
            //DateTime _dteTimeNow = UniMana.Lib.Sys.SysDateTime.Now;
            //UniMana.Lib.Hoa.Training_Score_TaskInputInfo infoTask = new Lib.Hoa.Training_Score_TaskInputInfo(
            //                                                m_gPersonelKey, m_iSubjectClassKey, _dteTimeNow);
            //if (infoTask.Properties.Personnel_PersonnelKey == m_gPersonelKey)
            //{
            //    //m_bLockRow = false;
            //    for (int i = 0; i < m_lScoreType.Count; i++)
            //    {
            //        int k = m_dtInfo.Columns.Count + i * 3;
            //        if (grvDatabaseColumns.FocusedRowHandle == k)
            //        {
            //            if (m_bLockRow == false)
            //            {
            //                Training_SubjectClass_ScoreTypeInfo info = new Training_SubjectClass_ScoreTypeInfo(
            //                                                            m_iSubjectClassKey, m_lScoreType[i]);
            //                if (info.Properties.Lock == false || info.Properties.Lock == null)
            //                {
            //                    e.Cancel = false;
            //                    m_bLockRow = true;
            //                    //m_iRowLock = k;
            //                    break;
            //                }
            //                else
            //                {
            //                    e.Cancel = true;
            //                }
            //            }
            //        }
            //        else
            //        {
            //            e.Cancel = true;
            //        }
            //    }
                
            //}
            if (grvDatabaseColumns.FocusedRowHandle == m_iPosition)
            {
                e.Cancel = false;
            }
            else
            {
                e.Cancel = true;
            }
            ////Cot diem ket thuc mon
            ////if (grvDatabaseColumns.FocusedRowHandle == grvDatabaseColumns.RowCount - 4)
            ////{
            ////    e.Cancel = false;
            ////    return;
            ////}
            ////
            //if (grvDatabaseColumns.FocusedRowHandle == grvDatabaseColumns.RowCount - 2)
            //{
            //    e.Cancel = true;
            //    return;
            //}
        }
    }
}